package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.modules.system.domain.dto.TeskandreadDto;
import me.zhengjie.modules.system.service.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequiredArgsConstructor
@Api(tags = "学生任务管理")
@RequestMapping("/api/teskAndstudent")
public class TeskAndstudentController {

    private final TeskAndstudentService teskAndstudentService;

    private final TeskandmessageService teskandmessageService;

    private final TeskandreadService teskandreadService;

    private final TeskandresourceService teskandresourceService;

    private final TeskandactiveService teskandactiveService;

    private final TeachCompleteService teachCompleteService;

    private final ResourceCompleteService resourceCompleteService;

    private final TeskExerciseSetCompleteService teskExerciseSetCompleteService;

    private final MessageCompleteService messageCompleteService;

    private final TeachchapterService teachchapterService;

    private final TeachsubsectionService teachsubsectionService;

    private final TeskService teskService;

    private final ResourceService resourceService;

    private final TeachService teachService;

    //学生获取任务列表
    @GetMapping("/getUserTeskList")
    @Log("学生获取任务列表")
    @ApiOperation("学生获取任务列表")
    public ResponseEntity<TeskStudent> getUserTeskList(@RequestParam("type") Integer type){

        TeskStudent teskStudent = new TeskStudent();

        List<Tesk> teskList =  teskAndstudentService.getUserTeskList();


        List<Tesk> teskList1 = new ArrayList<>();
        List<Tesk> teskList2 = new ArrayList<>();
        List<Tesk> teskList3 = new ArrayList<>();
        List<Tesk> teskList4 = new ArrayList<>();

        List<Integer> teskIds1 = new ArrayList<>();
        List<Integer> teskIds2 = new ArrayList<>();
        List<Integer> teskIds3 = new ArrayList<>();
        List<Integer> teskIds4 = new ArrayList<>();
        for(Tesk tesk:teskList){
            if(tesk.getType() == 0){

                teskIds1.add(tesk.getId());
                teskList1.add(tesk);
            } else if (tesk.getType() == 1){

                teskIds2.add(tesk.getId());
                teskList2.add(tesk);
            } else if (tesk.getType() == 2){

                teskIds3.add(tesk.getId());
                teskList3.add(tesk);
            } else if (tesk.getType() == 3){

                teskIds4.add(tesk.getId());
                teskList4.add(tesk);
            }
        }


        if(teskIds1.size()>0){
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.in("teskId",teskIds1);
            List<Teskandmessage> teskandmessageList = teskandmessageService.list(queryWrapper);
            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("teskId",teskIds1);
            List<MessageComplete> messageCompleteList = messageCompleteService.list(queryWrapper1);


            for(Tesk tesk:teskList1){
                for(Teskandmessage teskandmessage:teskandmessageList){
                    if(tesk.getId() == teskandmessage.getTeskid()){
                        tesk.setContent(teskandmessage.getContent());
                    }
                }
                for(MessageComplete messageComplete :messageCompleteList){
                    if(messageComplete.getTeskId() == tesk.getId()){

                        tesk.setState(messageComplete.getState());
                    }
                }
            }
        }
        if(teskIds2.size()>0){
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.in("teskId",teskIds2);
            List<Teskandread> teskandreadList = teskandreadService.list(queryWrapper);
//            teskIds2
            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("teskId",teskIds2);
            List<TeachComplete>  teachCompletes = teachCompleteService.list(queryWrapper1);

            for(Tesk tesk:teskList2){
                List<TeskandreadDto> teachArr = new ArrayList<>();
                for(Teskandread item:teskandreadList){
                    if(item.getTeskid() == tesk.getId()){
                        List<Integer> teachChapterArr  = new ArrayList<>();
                        List<Integer> teachSubsectionArr  = new ArrayList<>();
                        if(item.getTeachChapterId()!=null){
                            teachChapterArr.add(item.getTeachChapterId());

                        }
                        if(item.getTeachSubsectionId()!=null){
                            teachSubsectionArr.add(item.getTeachSubsectionId());
                        }

                        if(teachArr.size() == 0){

                            TeskandreadDto teskandreadDto = new TeskandreadDto();
                            teskandreadDto.setTeachId(item.getTeachid());
                            teskandreadDto.setTeachChapterArr(teachChapterArr);
                            teskandreadDto.setTeachSubsectionArr(teachSubsectionArr);
                            teachArr.add(teskandreadDto);

                        } else {
                            Integer status = 0;
                            for(TeskandreadDto item1:teachArr){
                                if(Objects.equals(item1.getTeachId(), item.getTeachid())){
                                    status = 1;
                                    List<Integer> currentTeachChapterArr = new ArrayList<>();
                                    Collections.addAll(currentTeachChapterArr, teachChapterArr.toArray(new Integer[0]));
                                    Collections.addAll(currentTeachChapterArr, item1.getTeachChapterArr().toArray(new Integer[0]));
                                    List<Integer> currentteachSubsectionArr = new ArrayList<>();
                                    Collections.addAll(currentteachSubsectionArr, teachSubsectionArr.toArray(new Integer[0]));
                                    Collections.addAll(currentteachSubsectionArr, item1.getTeachSubsectionArr().toArray(new Integer[0]));
                                    item1.setTeachChapterArr(currentTeachChapterArr);
                                    item1.setTeachSubsectionArr(currentteachSubsectionArr);
                                }
                            }
                            if(status == 0){
                                TeskandreadDto teskandreadDto = new TeskandreadDto();
                                teskandreadDto.setTeachId(item.getTeachid());
                                teachChapterArr.add(item.getTeachChapterId());
                                teachSubsectionArr.add(item.getTeachSubsectionId());

                                teskandreadDto.setTeachChapterArr(teachChapterArr);
                                teskandreadDto.setTeachSubsectionArr(teachSubsectionArr);

                                teachArr.add(teskandreadDto);
                            }
                        }
                    }
                }
                for(TeachComplete teachComplete:teachCompletes){
                    if(teachComplete.getTeskId() == tesk.getId()){
                        tesk.setState(teachComplete.getState());
                    }

                }
                List<Teachchapter> teachchapterList = new ArrayList<>();
                List<Teachsubsection> teachsubsectionList = new ArrayList<>();

                List<Integer> teachchapterIdList = new ArrayList<>();
                List<Integer> teachsubsectionIdList = new ArrayList<>();
                List<Integer> teachIds = new ArrayList<>();
                List<Teach> teachlist = new ArrayList<>();

                for(TeskandreadDto teskandreadDto:teachArr){
                    teachchapterIdList = Stream.concat(teskandreadDto.getTeachChapterArr().stream(), teachchapterIdList.stream())
                            .collect(Collectors.toList());
                    teachsubsectionIdList = Stream.concat(teskandreadDto.getTeachSubsectionArr().stream(), teachsubsectionIdList.stream())
                            .collect(Collectors.toList());

                }

                if(teachchapterIdList.size()!=0){
                    teachchapterList = teachchapterService.listByIds(teachchapterIdList);

                }

                if(teachsubsectionIdList.size()!=0){
                    teachsubsectionList = teachsubsectionService.listByIds(teachsubsectionIdList);

                }

                for(TeskandreadDto teskandreadDto:teachArr){
                    List<Chaptersubsection> chaptersubsectionList = new ArrayList<>();
                    List<Teachsubsection> teachsubsections = new ArrayList<>();

                    List<Teachchapter> teachcharts =  new ArrayList<>();
                    for(Teachchapter teachchapter : teachchapterList){

                        if(containsElement(teskandreadDto.getTeachChapterArr(), Math.toIntExact(teachchapter.getId()))){
                            teachcharts.add(teachchapter);

                            Chaptersubsection chaptersubsection = new Chaptersubsection();
                            chaptersubsection.setId(teachchapter.getId());
                            chaptersubsection.setName(teachchapter.getName());
                            chaptersubsection.setChapterId(teachchapter.getId());
                            chaptersubsection.setType(0);

                            chaptersubsectionList.add(chaptersubsection);
                        }
                        if(!containsElement(teachIds, teachchapter.getTeachid())){
                            teachIds.add(teachchapter.getTeachid());
                        }
                    }
                    for(Teachsubsection teachsubsection : teachsubsectionList){

                        if(containsElement(teskandreadDto.getTeachSubsectionArr(), Math.toIntExact(teachsubsection.getId()))){
                            teachsubsections.add(teachsubsection);
                            Chaptersubsection chaptersubsection = new Chaptersubsection();
                            chaptersubsection.setId(teachsubsection.getId());
                            chaptersubsection.setName(teachsubsection.getName());
                            chaptersubsection.setChapterId(teachsubsection.getTeachchapterid());
                            chaptersubsection.setSubsectionId(teachsubsection.getId());
                            chaptersubsection.setType(1);
                            chaptersubsectionList.add(chaptersubsection);
                        }

                        if(!containsElement(teachIds, Math.toIntExact(teachsubsection.getTeachid()))){
                            teachIds.add(Math.toIntExact(teachsubsection.getTeachid()));
                        }
                    }

                    Collections.sort(chaptersubsectionList, new Comparator<Chaptersubsection>() {
                        @Override
                        public int compare(Chaptersubsection o1, Chaptersubsection o2) {
                            return Integer.compare(Math.toIntExact(o1.getChapterId()), Math.toIntExact(o2.getChapterId()));
                        }
                    });
                    if(teachIds.size()>0){
                        teachlist = teachService.listByIds(teachIds);
                    }
                    if(teachlist.size()>0 && teachlist.get(0)!=null){
                        teskandreadDto.setName(teachlist.get(0).getName());
                    }

                    teskandreadDto.setTeachsubsectionNameArr(teachsubsections);
                    teskandreadDto.setTeachchapterNameArr(teachcharts);
                    teskandreadDto.setChaptersubsectionArr(chaptersubsectionList);
                }


                tesk.setTeachArrId(teachArr);
            }
        }
        if(teskIds3.size()>0){


            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.in("teskId",teskIds3);
            List<Teskandresource> teskandresourceList = teskandresourceService.list(queryWrapper);

            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("teskId", teskIds3);
            List<ResourceComplete>  resourceCompletes = resourceCompleteService.list(queryWrapper1);
            for(Tesk tesk:teskList3){
                List<String> resourceIds = new ArrayList<>();
                for(Teskandresource teskandresource:teskandresourceList){
                    if(tesk.getId() == teskandresource.getTeskid()){
                        resourceIds.add(teskandresource.getResourceid());
                    }
                }
                for(ResourceComplete item:resourceCompletes){
                    if(item.getTeskId() == tesk.getId()){
                        tesk.setState(item.getState());
                    }
                }
                if(resourceIds.size()>0){
                    List<Resource> resourceList = resourceService.listByIds(resourceIds);
                    tesk.setResourceArr(resourceList);
                }

                tesk.setResourceArrId(resourceIds);

            }
        }
        if(teskIds4.size()>0){
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.in("teskId",teskIds4);
            List<Teskandactive> teskandactiveList = teskandactiveService.list(queryWrapper);
            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("teskId",teskIds4);
            List<TeskExerciseSetComplete> teskExerciseSetCompleteList = teskExerciseSetCompleteService.list(queryWrapper1);
            for(Tesk tesk:teskList4){
                List<Integer> activeIds = new ArrayList<>();
                for(Teskandactive teskandactive:teskandactiveList){

                    if(tesk.getId() == teskandactive.getTeskid()){
                        activeIds.add(teskandactive.getExercisesetid());
                    }
                }
                for(TeskExerciseSetComplete item:teskExerciseSetCompleteList){
                    if(item.getTeskId() == tesk.getId()){
                        tesk.setState(item.getState());
                    }
                }
                tesk.setExerciseSetArrId(activeIds);
            }


        }

        for(Tesk tesk:teskList1){
            if( tesk.getState() == null || tesk.getState() == 0){
                if(teskStudent.getMessagesCount()==null){

                    teskStudent.setMessagesCount(1);
                } else {
                    teskStudent.setMessagesCount(teskStudent.getMessagesCount() + 1);
                }

            }
        }

        for(Tesk tesk:teskList2){
            if(tesk.getState() == null || tesk.getState() == 0){
                if(teskStudent.getReadyTeskCount()==null){
                    teskStudent.setReadyTeskCount(1);
                } else {
                    teskStudent.setReadyTeskCount(( teskStudent.getReadyTeskCount() + 1));
                }

            }
        }

        for(Tesk tesk:teskList3){
            if(tesk.getState() == null || tesk.getState() == 0){
                if(teskStudent.getResourcesCount()==null){
                    teskStudent.setResourcesCount(1);
                } else {
                    teskStudent.setResourcesCount(( teskStudent.getResourcesCount() + 1));
                }
            }
        }

        for(Tesk tesk:teskList4){
            if(tesk.getState() == null || tesk.getState() == 0){

                if(teskStudent.getActiveCount()==null){
                    teskStudent.setActiveCount(1);
                } else {
                    teskStudent.setActiveCount(( teskStudent.getActiveCount() + 1));

                }
            }
        }

        if(type == 0){

            teskStudent.setMessagesTeskList(teskList1);
        } else if(type == 1){

            teskStudent.setReadyTeskList(teskList2);
        } else if(type == 2){

            teskStudent.setResourcesTeskList(teskList3);
        } else {

            teskStudent.setActiveTeskList(teskList4);
        }

        return new ResponseEntity<>(teskStudent, HttpStatus.OK);
    }


    public boolean containsElement(List<Integer> array, Integer target) {
        for (int i = 0; i < array.size(); i++) {
            if (array.get(i) ==  target) {
                return true;
            }
        }
        return false;
    }


    //教师获取学生任务
    //学生获取任务列表
    @GetMapping("/getStudentTeskList")
    @Log("教师获取学生任务")
    @ApiOperation("教师获取学生任务")
    public ResponseEntity<TeskStudent> getStudentTeskList(@RequestParam(name = "studentId", required = true) Integer studentId,@RequestParam(name = "type", required = true) Integer type){

        TeskStudent teskStudent = new TeskStudent();
        List<Tesk> teskList = teskAndstudentService.getStudentTeskList(studentId);
        List<Tesk> teskList1 = new ArrayList<>();
        List<Tesk> teskList2 = new ArrayList<>();
        List<Tesk> teskList3 = new ArrayList<>();
        List<Tesk> teskList4 = new ArrayList<>();

        List<Integer> teskIds1 = new ArrayList<>();
        List<Integer> teskIds2 = new ArrayList<>();
        List<Integer> teskIds3 = new ArrayList<>();
        List<Integer> teskIds4 = new ArrayList<>();
        for(Tesk tesk:teskList){
            if(tesk.getType() == 0){

                teskIds1.add(tesk.getId());
                teskList1.add(tesk);
            } else if (tesk.getType() == 1){

                teskIds2.add(tesk.getId());
                teskList2.add(tesk);
            } else if (tesk.getType() == 2){

                teskIds3.add(tesk.getId());
                teskList3.add(tesk);
            } else if (tesk.getType() == 3){

                teskIds4.add(tesk.getId());
                teskList4.add(tesk);
            }
        }


        if(teskIds1.size()>0){
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.in("teskId",teskIds1);
            List<Teskandmessage> teskandmessageList = teskandmessageService.list(queryWrapper);
            for(Tesk tesk:teskList1){
                for(Teskandmessage teskandmessage:teskandmessageList){
                    if(tesk.getId() == teskandmessage.getTeskid()){
                        tesk.setContent(teskandmessage.getContent());
                    }
                }
            }
        }
        if(teskIds2.size()>0){
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.in("teskId",teskIds2);
            List<Teskandread> teskandreadList = teskandreadService.list(queryWrapper);

            for(Tesk tesk:teskList2){
                List<TeskandreadDto> teachArr = new ArrayList<>();
                for(Teskandread item:teskandreadList){
                    if(item.getTeskid() == tesk.getId()){
                        List<Integer> teachChapterArr  = new ArrayList<>();
                        List<Integer> teachSubsectionArr  = new ArrayList<>();
                        if(item.getTeachChapterId()!=null){
                            teachChapterArr.add(item.getTeachChapterId());

                        }
                        if(item.getTeachSubsectionId()!=null){
                            teachSubsectionArr.add(item.getTeachSubsectionId());
                        }

                        if(teachArr.size() == 0){

                            TeskandreadDto teskandreadDto = new TeskandreadDto();
                            teskandreadDto.setTeachId(item.getTeachid());


                            teskandreadDto.setTeachChapterArr(teachChapterArr);
                            teskandreadDto.setTeachSubsectionArr(teachSubsectionArr);

                            teachArr.add(teskandreadDto);
                        } else {
                            Integer status = 0;
                            for(TeskandreadDto item1:teachArr){
                                if(Objects.equals(item1.getTeachId(), item.getTeachid())){
                                    status = 1;

                                    List<Integer> currentTeachChapterArr = new ArrayList<>();
                                    Collections.addAll(currentTeachChapterArr, teachChapterArr.toArray(new Integer[0]));
                                    Collections.addAll(currentTeachChapterArr, item1.getTeachChapterArr().toArray(new Integer[0]));

                                    List<Integer> currentteachSubsectionArr = new ArrayList<>();
                                    Collections.addAll(currentteachSubsectionArr, teachSubsectionArr.toArray(new Integer[0]));
                                    Collections.addAll(currentteachSubsectionArr, item1.getTeachSubsectionArr().toArray(new Integer[0]));

                                    item1.setTeachChapterArr(currentTeachChapterArr);
                                    item1.setTeachSubsectionArr(currentteachSubsectionArr);
                                }
                            }
                            if(status == 0){
                                TeskandreadDto teskandreadDto = new TeskandreadDto();
                                teskandreadDto.setTeachId(item.getTeachid());
                                teachChapterArr.add(item.getTeachChapterId());
                                teachSubsectionArr.add(item.getTeachSubsectionId());

                                teskandreadDto.setTeachChapterArr(teachChapterArr);
                                teskandreadDto.setTeachSubsectionArr(teachSubsectionArr);

                                teachArr.add(teskandreadDto);
                            }
                        }
                    }
                }
                tesk.setTeachArrId(teachArr);
            }

        }
        if(teskIds3.size()>0){
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.in("teskId",teskIds3);
            List<Teskandresource> teskandresourceList = teskandresourceService.list(queryWrapper);
            for(Tesk tesk:teskList3){
                List<String> resourceIds = new ArrayList<>();
                for(Teskandresource teskandresource:teskandresourceList){
                    if(tesk.getId() == teskandresource.getTeskid()){
                        resourceIds.add(teskandresource.getResourceid());
                    }
                }
                tesk.setResourceArrId(resourceIds);
            }
        }
        if(teskIds4.size()>0){
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.in("teskId",teskIds4);
            List<Teskandactive> teskandactiveList = teskandactiveService.list(queryWrapper);
            for(Tesk tesk:teskList3){
                List<Integer> activeIds = new ArrayList<>();
                for(Teskandactive teskandactive:teskandactiveList){
                    if(tesk.getId() == teskandactive.getTeskid()){
                        activeIds.add(teskandactive.getExercisesetid());
                    }
                }
                tesk.setExerciseSetArrId(activeIds);
            }
        }

        if(type == 0){
            teskStudent.setMessagesTeskList(teskList1);
        } else if(type == 1){
            teskStudent.setReadyTeskList(teskList2);
        } else if(type == 2){
            teskStudent.setResourcesTeskList(teskList3);
        } else {
            teskStudent.setActiveTeskList(teskList4);
        }
        return new ResponseEntity<>(teskStudent, HttpStatus.OK);

    }

    //获取资源列表
    public ResponseEntity<TeskStudent> getTeskResourceList(@RequestParam("teskId") Integer teskId){

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teskId",teskId);
        List<Teskandresource> teskandresourceList = teskandresourceService.list(queryWrapper);
//        for(Tesk tesk:teskList3){
        Tesk tesk = teskService.getById(teskId);
        List<String> resourceIds = new ArrayList<>();
        for(Teskandresource teskandresource:teskandresourceList){
            if(tesk.getId() == teskandresource.getTeskid()){
                resourceIds.add(teskandresource.getResourceid());
            }
        }
        tesk.setResourceArrId(resourceIds);
        return null;
    }
}
